<template>
  <div class="layout-route">
      <div class="card card-search">
          <el-form :model="currentInfo.queryParams" ref="queryForm" inline label-width="100px">
              <el-form-item :label="currentInfo.label" prop="title">
                  <el-input v-model.trim="currentInfo.queryParams.title" :placeholder="`请输入${currentInfo.label}`" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
              </el-form-item>

              <el-form-item label=" ">
                  <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
          </el-form>
      </div>

      <div class="card">
          <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                  <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({}, 'add')">添加</el-button>
              </el-col>
          </el-row>
          <el-tabs v-model="activeId" type="border-card" @tab-click="handleQuery">
              <el-tab-pane v-for="item in tabs" :key="item.value" :label="item.label" :name="item.value">
                <el-table v-loading="loading" :data="currentInfo.tableData" border script>
                  <el-table-column label="ID" prop="id" min-width="100"></el-table-column>
                  <el-table-column :label="currentInfo.label" prop="title" min-width="200"></el-table-column>
                  <el-table-column label="状态" prop="status" min-width="100">
                    <template slot-scope="scope">
                        {{ scope.row.status | formatLabelByValueFuzzy(STATUS_LIST) }}
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" min-width="100">
                      <template slot-scope="scope">
                          <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                          <el-button type="text" @click="deleteItem(scope.row, scope.$index)" :disabled="loadingDelete">删除</el-button>
                      </template>
                  </el-table-column>
              </el-table>
              <pagination v-show="currentInfo.total > 0" :total="currentInfo.total" :page.sync="currentInfo.pageInfo.page" :limit.sync="currentInfo.pageInfo.limit" @pagination="getList"></pagination>
              </el-tab-pane>
          </el-tabs>
      </div>

      <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" :category="activeId" @success="handleQuery"></detail-dialog>
  </div>
</template>

<script>
import { accountopeningList, proxyownershipList, advplatformList,accountopeningDel, proxyownershipDel, advplatformDel } from '@/api/advertisement'
import { STATUS_LIST } from '../constant'
import DetailDialog from './components/DetailDialog.vue'
export default {
  name: 'AdvaccountAccountList',
  components: { DetailDialog },
  data() {
      return {
          // 遮罩层
          loading: true,
          activeId: '1',
          tabs: [
            { label: '开户主体', value: '1', fn: accountopeningList,fn_del: accountopeningDel, total: 0,
                pageInfo: {
                    page: 1,
                    limit: 10,
                },
                queryParams: {
                    title: undefined,
                },
            },
            { label: '代理归属', value: '2', fn: proxyownershipList,fn_del: proxyownershipDel, total: 0,
                pageInfo: {
                    page: 1,
                    limit: 10,
                } ,
                queryParams: {
                    title: undefined,
                },
            },
            { label: '广告平台', value: '3', fn: advplatformList,fn_del: advplatformDel, total: 0,
                pageInfo: {
                    page: 1,
                    limit: 10,
                },
                queryParams: {
                    title: undefined,
                },
            },
          ],
          STATUS_LIST,
          detailDialog: {
              visible: false,
              data: {},
              type: 'add'
          },
      }
  },
  created() {
      this.getList()
  },
  computed: {
    currentInfo() {
        return this.tabs.find(tab=>tab.value == this.activeId)
    }
  },
  methods: {
    deleteItem(item, index){
        this.$confirm(`是否确认删除?`, '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.loadingDelete = true
            console.log('item',item)
            const {fn_del } = this.currentInfo
            fn_del(item)
                .then(res => {
                    this.$message.success(res.msg)
                    this.resetQuery()
                })
                .finally(() => {
                    this.loadingDelete = false
                })
        }).catch(() => { })
        },
      showDetailDialog(row, type) {
          this.detailDialog.visible = true
          this.detailDialog.data = row
          this.detailDialog.type = type
      },
      //列表
      getList() {
          this.loading = true
          const { pageInfo, queryParams, fn } = this.currentInfo
          const qyparams = {
              ...pageInfo,
              ...queryParams,
          }
          fn(qyparams)
              .then(response => {
                  this.currentInfo.tableData = response.data.list
                  this.currentInfo.total = response.data.total
              })
              .finally(() => {
                  this.loading = false
              })
      },

      // 搜索按钮操作
      handleQuery() {
          this.currentInfo.pageInfo.page = 1
          this.getList()
      },

      // 重置按钮操作
      resetQuery() {
        //   this.resetForm('queryForm')
        this.currentInfo.queryParams.title = ''

          this.handleQuery()
      },
  },
}
</script>
<style scoped>
/* ::v-deep .el-tabs .el-tabs__content {
  padding: 0;
  border-bottom: 0;
}
.el-tabs--border-card {
  box-shadow: none;
  border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
  padding: 0;
  border-bottom: 0;
}
.table-wrap {
  padding: 15px;
  border-bottom: 1px solid #dfe4ed;
  border-left: 1px solid #dfe4ed;
  border-right: 1px solid #dfe4ed;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
} */
</style>
